package com.lyh.seckill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyh.seckill.mapper.SeckillOrderMapper;
import com.lyh.seckill.pojo.SeckillMessage;
import com.lyh.seckill.pojo.SeckillOrder;
import com.lyh.seckill.pojo.User;
import com.lyh.seckill.service.ISeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2022-11-04
 */
@Service
public class SeckillOrderServiceImpl extends ServiceImpl<SeckillOrderMapper, SeckillOrder> implements ISeckillOrderService {

    @Autowired
    @Resource
    private SeckillOrderMapper seckillOrderMapper;

    @Autowired
    @Resource
    private RedisTemplate redisTemplate;

    //获取秒杀结果 orderId:成功 -1:失败 0:排队中
    @Override
    public Long getResult(User user, Long goodsId) {
        //根据用户和商品获取秒杀订单
        SeckillOrder seckillOrder = seckillOrderMapper.selectOne(new QueryWrapper<SeckillOrder>().eq("user_id",user.getId()).eq("goods_id",goodsId));
        if(seckillOrder!=null){
            return  seckillOrder.getOrderId();
        }else if(redisTemplate.hasKey("isStockEmpty:"+goodsId)){
            return  -1L;
        }else{
            return  0L;
        }
//        return null;
    }
}
